from sqlalchemy import Select
from models.user import User
from app.app_init import db
from flask_login import login_user, logout_user


class UserService:

    def do_login(self, username, password):
        query = Select(User).where(User.username == username)
        attemped_user = db.session.scalar(query)
        if attemped_user and attemped_user.check_password_correcton(attempted_password=password):
            login_user(attemped_user)
            return True

        return False

    def do_logout(self):
        logout_user()